#include <REGX52.H>
#include <intrins.h>
#include "Delay.h"
#include "Key.h"
#include "Timer0.h"
#include "Timer1.h"
#include "Timer2.h"
#include "LCD1602.h"
#include "TLC549.h"


//模式选择，用以LCD显示并且按键太少需要控制的太多加以区分。
unsigned char Mode;
unsigned char KeyNum;

//时钟模块 Mode0
unsigned char Sec=00,Min=00,Hour=00;

//舵机模块Mode1
sbit Sev=P1^3;
unsigned char angle,Counterd,Compared;


//直流电机模块 Mode2
sbit Motor=P1^0;
sbit IN1=P1^2;
sbit IN2=P1^1;
unsigned char Counter,Compare;	//计数值和比较值，用于输出PWM
//TLC549

unsigned char jzad;
unsigned int jz;


void main()
{   
	Mode=0;
	IN1=1;
    IN2=0;	
	LCD_Init();
	Timer0_Init();
	Timer1_Init();
	Timer2_Init();
	Compare=50;
	Compared=1;
	
	while(1)
	{   
		KeyNum=Key();
		if(KeyNum==4)
		{
		   Mode++;
		   Mode%=4;
		}
		
		//舵机控制
		if(KeyNum==1&&Mode==1)
		{
			Compared=Compared+1;
			if(Compared==6)
			{
			Compared=1;
			}
				
		}
		switch(Compared)
		{
			case 1:angle=0;
			break;
			case 2:angle=38;
			break;
			case 3:angle=80;
			break;
			case 4:angle=121;
			break;
			case 5:angle=162;
			break;
		
		}
		
		
		//直流电机控制
	
		if(KeyNum==3&&Mode==2)//反转
		{
		   IN1=~IN1;
		   IN2=~IN2;
		}
		if(KeyNum==1&&Mode==2)//减速
		{
			Compare=Compare-5;
			if(Compare==0)
			{
			Compare=50;
			}
				
		}
		if(KeyNum==2&&Mode==2)//加速
		{
			Compare=Compare+5;
			if(Compare==100)
			{
			Compare=50;
			}
				
		}
		//TLC549
	     jzad=TLC549_ADC();
		 jz=jzad*1.97;
		
		
		
		
//LCD显示函数
	    if(Mode==0)//LCD显示时钟
		{   
			LCD_ShowString(1,1,"Clock:    ");	//上电显示静态字符串
	        LCD_ShowString(2,1,"  :  :");
		    LCD_ShowNum(2,1,Hour,2);	//显示时分秒
		    LCD_ShowNum(2,4,Min,2);
		    LCD_ShowNum(2,7,Sec,2);
	    }
		
        if(Mode==1)//LCD显示舵机状态这里调整半天实在无法精确角度
		{
			LCD_ShowString(1,1,"D:      ");
		    LCD_ShowNum(1,3,angle,3);
			LCD_ShowString(2,1,"           ");	//清除时分秒 发现秒表有残留，用笨方法清除
		   
		}
		
		if(Mode==2)//LCD显示直流电机状态其中D=1代表正转
		{
			LCD_ShowString(1,1,"D:  W:  %");
		    LCD_ShowNum(1,3,IN1,1);
		    LCD_ShowNum(1,7,Compare,2);
			LCD_ShowString(2,1,"           ");	//清除时分秒
		   
		}
		if(Mode==3)//LCD显示电压测量值（这里不知道为啥电阻值拉很低会出现高电压显示）
		{
			LCD_ShowString(1,1,"v= .  V    ");
		    LCD_ShowNum(1,3,0x30+jz%1000/100,1);
		    LCD_ShowNum(1,5,0x30+jz%100/10,1);
			LCD_ShowNum(1,6,0x30+jz%10,1);
			LCD_ShowString(2,1,"           ");	//清除时分秒
		   
		}
	}
}
//中断函数 定时器0用以电机 定时器1用以秒表 定时器2用以舵机
void Timer0_Routine() interrupt 1//直流电机中断
{
	TL0 = 0x9c;		//设置定时初值
	TH0 = 0xFF;		//设置定时初值
	Counter++;
	Counter%=100;	//计数值变化范围限制在0~99
	if(Counter<Compare)	//计数值小于比较值
	{
		Motor=1;		//输出1
	}
	else				//计数值大于比较值
	{
		Motor=0;		//输出0
	}

    
}
void Timer1_Routine() interrupt 3//时钟中断
{
	static unsigned int T1Count;
	TL1 = 0x18;		//设置定时初值
	TH1 = 0xFC;		//设置定时初值
	T1Count++;
	if(T1Count>=1000)	//定时器分频，1s
	{
		T1Count=0;
		Sec++;			//1秒到，Sec自增
		if(Sec>=60)
		{
			Sec=0;		//60秒到，Sec清0，Min自增
			Min++;
			if(Min>=60)
			{
				Min=0;	//60分钟到，Min清0，Hour自增
				Hour++;
				if(Hour>=24)
				{
					Hour=0;	//24小时到，Hour清0
				}
			}
		}
	}
}
void Timer2_Routine() interrupt 5
{   TF2=0;
	Counterd++;
	Counterd%=40;
   if(Counterd<Compared)	//计数值小于比较值
	{
		Sev=1;		//输出1
	}
	else				//计数值大于比较值
	{
		Sev=0;		//输出0
	}


}

